<template>
  <el-scrollbar height="400px">
    <el-row>
      <el-col :span="4">
        <div class="grid-content bg-purple">
          序号
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content bg-purple-light">
          项目ID
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          项目名称
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple-light">
          完成情况
        </div>
      </el-col>
    </el-row>

    <el-row v-for="(value,index) in res2 " :key="index">
      <el-col :span="4">
        <div class="grid-content bg-purple">
          {{ index }}
        </div>
      </el-col>
      <el-col :span="4">
        <div class="grid-content  bg-purple-light">
         {{ value.id }}
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content bg-purple">
          {{ value.projectName }}
        </div>
      </el-col>
      <el-col :span="8">
        <div class="grid-content  bg-purple-light">
          <el-progress :percentage="percentage[index]" :format="format" />
        </div>
      </el-col>
    </el-row>

  </el-scrollbar>
</template>

<script setup lang="ts">

import getProjectByUserId from "@/api/project/getProjectByUserId";
import {ref, watch} from "vue";
import getProjectPercentage from "@/api/project/getProjectPercentage";

 let status :number = parseInt(sessionStorage.getItem('project.status'));
if (isNaN(status)){
  status = null;
}
const res = getProjectByUserId(status);
let res2 = ref<[]>([]);
const percentage = ref<[]>([]);



res.then((data) => {
  for (let i = 0; i < data.length; i++) {
    res2.value?.push(data[i])
  }
})

watch((res2.value),()=>{
  for (let i = 0; i < res2.value.length; i++){
    if (res2.value[i].status == 0 ||res2.value[i].status== -1){
      percentage.value.push(0);
    }else {
      percentFun(res2.value[i].id);
    }
  }
})



const format = (percentage: number) => (percentage === 100 ? 'success' : `${percentage}%`)
const percentFun = async (id: number) => {

  await getProjectPercentage(id)
      .then((data) => {
        percentage.value.push(data * 100);
      })

}
</script>

<style scoped>

.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.scrollbar-demo-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  margin: 10px;
  text-align: center;
  border-radius: 4px;
  background: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
}
</style>